﻿using FirstFloor.ModernUI.Presentation;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using Microsoft.Practices.Unity;
using EasyInvoice.ApplicationContext;
using appCtx = EasyInvoice.ApplicationContext.AppContext;
using EasyInvoice.SqlCEDAL;
using EasyInvoice.Services;
using System.Drawing;
using System.Windows.Media.Imaging;

namespace EasyInvoice.WPF
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            
            //register Types
            appCtx.Instance.IoC.RegisterType<SqlCEDAL.IClients, SqlCEDAL.Clients>();
            appCtx.Instance.IoC.RegisterType<SqlCEDAL.IUsers, SqlCEDAL.Users>();
            appCtx.Instance.IoC.RegisterType<SqlCEDAL.IInvoices, SqlCEDAL.Invoices>();
            appCtx.Instance.IoC.RegisterType<SqlCEDAL.IInvoiceDetail, SqlCEDAL.InvoiceDetail>();
            appCtx.Instance.IoC.RegisterType<SqlCEDAL.IDatabase, SqlCEDAL.Database>();

            //Setting Culture for application
            System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("en-US");
            System.Globalization.DateTimeFormatInfo dateTimeInfo = new System.Globalization.DateTimeFormatInfo()
            {
                ShortDatePattern = "yyyy-MMMM-dd",
                ShortTimePattern = "hh:mm:ss tt",
                TimeSeparator = ":",
                MonthNames = System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat.MonthNames,
                AbbreviatedMonthNames = System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat.AbbreviatedMonthNames,
                DateSeparator = " "
            };

            cultureInfo.DateTimeFormat = dateTimeInfo;
            System.Threading.Thread.CurrentThread.CurrentCulture = cultureInfo;

            MainWindow window = new MainWindow();
            BitmapImage logo = new BitmapImage();
            logo.BeginInit();
            logo.UriSource = new Uri("pack://application:,,,/EasyInvoice;component/Resources/MainWindowIcon.png", UriKind.RelativeOrAbsolute);
            logo.EndInit();
            window.Icon = logo;
            //Load application settings
            try
            {
                if (EasyInvoice.WPF.Properties.Settings.Default.SelectedTheme != null)
                    AppearanceManager.Current.ThemeSource = EasyInvoice.WPF.Properties.Settings.Default.SelectedTheme;
                if (EasyInvoice.WPF.Properties.Settings.Default.SelectedFontSize != null)
                    AppearanceManager.Current.FontSize = EasyInvoice.WPF.Properties.Settings.Default.SelectedFontSize;

                AppearanceManager.Current.AccentColor = EasyInvoice.WPF.Properties.Settings.Default.SelectedAccentColor;
                //Creating Connection string for SQLCe
                //ModelCE.ConnectionManager.ConnectionString = string.Format("Data Source={0}; Password={1};",
                //    EasyInvoice.WPF.Properties.Settings.Default.DatabaseFileLocation,
                //    EasyInvoice.WPF.Properties.Settings.Default.DatabasePassword);

                //Accesing Database for starting connection before displaying main window
                //new ModelCE.Client().GetAllCE();

                //new FirstFloor.ModernUI.Windows.Controls.ModernWindow()
                //{
                //    Style = (Style)App.Current.Resources["EmptyWindow"],
                //    Width = 600,
                //    Height = 800,
                //    Content = new Window1(),
                //    WindowStyle = WindowStyle.SingleBorderWindow,
                //    WindowStartupLocation = WindowStartupLocation.CenterScreen,
                //    ResizeMode = ResizeMode.NoResize

                //}.Show();
                //System.Xml.XmlDocument XMLdoc = new System.Xml.XmlDocument();
                // XMLdoc.Load("C:\\Users\\lpe\\Documents\\Visual Studio 2012\\Projects\\pdftemplate-30762\\TestApp\\template.xml");
                // new FirstFloor.ModernUI.Windows.Controls.ModernDialog()
                // {
                //     Content = new Content.ReportDesigner()
                     
                // }.Show();

                //Creating Connection string for SQLCe
                SqlCEDAL.Properties.Settings.Default["ConnectionString"] = string.Format("Data Source=\"{0}\"; Password='{1}';",
                    EasyInvoice.WPF.Properties.Settings.Default.DatabaseFileLocation,
                     EasyInvoice.WPF.Properties.Settings.Default.DatabasePassword);
                SqlCEDAL.Properties.Settings.Default.Save();

                EasyInvoice.WPF.Properties.Settings.Default.AuthenticatedUser = null;

                //InitializingConnection
                Repository.Reset();

                // ((ViewModel.MainWindowViewModel)window.DataContext).SelectedLink = new Uri("/Content/LogIn.xaml", UriKind.Relative);
                //((ViewModel.MainWindowViewModel)window.DataContext).SelectedLink = new Uri("/Content/UserList.xaml", UriKind.Relative);

            }
            catch (System.NullReferenceException)
            {
                ((ViewModel.MainWindowViewModel)window.DataContext).SelectedLink = new Uri("/Content/SettingsAppearance.xaml", UriKind.Relative);
            }
            //catch (System.Data.SqlServerCe.SqlCeException)
            //{
            //    ((ViewModel.MainWindowViewModel)window.DataContext).SelectedLink = new Uri("/Pages/Settings.xaml", UriKind.Relative); 
            //}
            catch (TypeInitializationException)
            {
                ((ViewModel.MainWindowViewModel)window.DataContext).SelectedLink = new Uri("/Content/SettingsDatabase.xaml", UriKind.Relative);
            }

            window.Show();
            //new FirstFloor.ModernUI.Windows.Controls.ModernDialog()
            //{
            //    Content = new Content.ReportDesigner()

            //}.Show();
           //((ViewModel.SettingsViewModel)((Pages.Settings)((System.Windows.Controls.Grid)window.Content).Children[0]).DataContext).SelectedSource = new Uri("/Content/SettingsExport.xaml", UriKind.Relative);
            
        }
    }
}
